package com.mtjx.service.impl;

//import com.mtjx.entitys.LoginUser;

import com.mtjx.entitys.dto.LoginDTO;
import com.mtjx.entitys.entitys.LoginUser;
import com.mtjx.entitys.entitys.User;
import com.mtjx.mapper.UserMapper;
import com.mtjx.resp.VfResult;
import com.mtjx.service.LoginService;
import com.mtjx.utils.JwtUtil;
import com.mtjx.utils.RedisCache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author 33117
 * @version 1.0
 * @data 2025/8/29 10:08
 */
@Service
public class LoginSserviceImpl implements LoginService {
    // 设置redis有效期
    public static final Long REDIS_TTL = 60 * 60 * 1000L;

   /* @Resource
    private RedisCache redisCache;*/
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserMapper userMapper;

    @Override
    public VfResult login(LoginDTO loginDTO) {
        // 进行认证

        User user = userMapper.selectUser(loginDTO.getUsername(), loginDTO.getPassword());
        if (Objects.isNull(user)) {
            return VfResult.errorMsg("用户不存在");
        }
        List<String> strings = userMapper.queryAuthorization(loginDTO.getUsername());

        // 生成jwt

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("token", "陌");
        hashMap.put("username", user.getUsername());
        String jwt = JwtUtil.createJWT("陌",REDIS_TTL,hashMap );
        // 存redis

        redisTemplate.opsForValue().set(user.getUsername(), strings, Math.toIntExact(REDIS_TTL), TimeUnit.MILLISECONDS);
        return VfResult.build(200, "登录成功", jwt);
    }
}
